﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.Ciclos;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Distritos
{
	public partial class DistritosViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Ciclo> _ciclos;

		/// <summary>
		/// 
		/// </summary>
		private Ciclo _ciclo;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Ciclo> Ciclos
		{
			get
			{
				return _ciclos;
			}
			private set
			{
				_ciclos = value;
				if (_ciclos != null)
					RaisePropertyChanged("Ciclos");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Ciclo Ciclo
		{
			get
			{
				return _ciclo;
			}
			private set
			{
				_ciclo = value;
				if (_ciclo != null)
					RaisePropertyChanged("Ciclo");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsCicloReady
		{
			get
			{
				return Ciclo != null;
			}
		}

		#endregion

		#region Actions

		/// <summary>
		/// 
		/// </summary>
		private void LoadCiclos()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					ICicloService cicloService = IoCFactory.Resolve<ICicloService>();
					e.Result = cicloService.FindAllCiclos();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Ciclos = e.Result as List<Ciclo>;
					else if (e.Error != null)
						MessageBox.Show(
							_distritosView,
							e.Error.Message,
							WindowTitles.DistritosWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentCiclo(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null)
				if (bindingSource.Current != null)
					Ciclo = bindingSource.Current as Ciclo;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnCicloPropertyChanged()
		{
			if (IsDistritoReady)
				LoadDistritoPersonal();
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnCiclosPropertyChanged()
		{
			_distritosView.ciclosBindingSource.DataSource = Ciclos;
			_distritosView.ciclosBindingSource.CurrentChanged += SetCurrentCiclo;
			SetCurrentCiclo(_distritosView.ciclosBindingSource, new EventArgs());
		}

		#endregion

	}
}
